package com.ssyt.hiring.company.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.hiring.common.Constants;
import com.ssyt.hiring.common.PageModel;
import com.ssyt.hiring.company.entity.PositionEntity;
import com.ssyt.hiring.company.service.PositionService;

public class PositionServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private PositionService positionService = new PositionService();

	public PositionServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constants.ENCODE_UTF8);
		String command = request.getParameter("command");
		if ("listPosition".equals(command)) {
			listPosition(request, response);
		}
	}

	/**
	 * 查询所有的职位信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listPosition(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 分页信息
		int pageNo = Constants.DEFAULT_PAGE_NO;
		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr != null) {
			pageNo = Integer.parseInt(pageNoStr);
		}
		int pageSize = Constants.DEFAULT_PAGE_SIZE;
		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr != null) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		// 条件信息
		String searchText = request.getParameter("searchText");

		PageModel<PositionEntity> pageModel = positionService.listPosition(
				searchText, pageNo, pageSize);

		request.setAttribute("pageModel", pageModel);
		request.setAttribute("searchText", searchText);
		request.getRequestDispatcher("/WEB-INF/view/common/hiring.jsp")
				.forward(request, response);
	}
}
